package com.atguigu02._class;

import org.junit.Test;

/**
 * ClassName: ClassTest
 * Description:
 *
 * @Author liu
 * @Create 2025/4/26 16:23
 * @Version 1.0
 */
public class ClassTest {
    /*
    获取Class实例的几种方法
     */
    @Test
    public void test1()throws Exception{
        //1.调用运行时类的静态属性：class
        Class clazz1 = User.class;
        System.out.println(clazz1);

        //2.运用运行时类的对象的getClass()
        User u1 = new User();
        Class clazz2 = u1.getClass();

        //3.调用Class的静态方法forName(String className)
        String className = "com.atguigu02._class.User";//copy special -->copy Reference
        Class clazz3 = Class.forName(className);

        System.out.println(clazz1 == clazz2);//true
        System.out.println(clazz2 == clazz3);//true因为都存在于方法区内

      //4.使用类的加载器的方式（了解）
        Class clazz4 = ClassLoader.getSystemClassLoader().loadClass("com.atguigu02._class.User");
        System.out.println(clazz1 = clazz3);//true
    }
}
